<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="formRef"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="分类名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="父级类目" prop="parentId">
      <el-select v-model="ruleForm.parentId" placeholder="请选择父级类目">
        <el-option label="顶级类目" value="0-0"></el-option>
        <el-option
          v-for="item in cateList"
          :label="item.name"
          :value="item.objectId"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否上架" prop="stat">
      <el-switch v-model="ruleForm.stat"></el-switch>
    </el-form-item>
    <el-form-item label="分类图标" prop="icon">
      <el-input v-model="ruleForm.icon"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { categoryPost, categoryGet } from "@/api/job.js";
export default {
  data() {
    return {
      ruleForm: {
        name: "开发",
        parentId: "0-0",
        stat: true,
        icon: "img.png",
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        parentId: [
          { required: true, message: "请勾选父级类目", trigger: "change" },
        ],
        stat: [
          { required: true, message: "请勾选上架状态", trigger: "change" },
        ],
        icon: [{ required: true, message: "请上传分类图标", trigger: "blur" }],
      },
      cateList: [], //下拉列表数据
    };
  },
  created() {
    categoryGet().then((res) => {
      console.log(res);
      this.cateList = res.data.results;
    });
  },
  methods: {
    submitForm(formName) {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          categoryPost(this.ruleForm).then((res) => {
            console.log("新增成功", res);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
